//
//  FSSwitchMenuChangeTrend.m
//  cocoapodText
//
//  Created by apple on 16/8/12.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
#import <Foundation/Foundation.h>
#import "FSSwitchMenuChangeTrend.h"
@interface FSSwitchMenuChangeTrend()
@end

@implementation FSSwitchMenuChangeTrend
-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        //自动伸缩高度可伸缩，右边距可以伸缩
        self.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleRightMargin;
        //设置背景图片
        [self setBackgroundColor:[UIColor whiteColor]];
        [self addTabItems];
    }
    return self;
}

//添加Tab选项卡
- (void)addTabItems
{
    [self addSingleTabWithText:@"区域" weight:1];
    [self addSingleTabWithText:@"分项" weight:2];
    [self addSingleTabWithText:@"行业" weight:3];
    [self changeTabItemStyle:[self->TabItem1 tag]];
}

- (void)addSingleTabWithText:(NSString *)text weight:(int)weight
{
    UILabel *tabItem=[[UILabel alloc]initWithFrame:CGRectMake(([ShiPei CGFloatMakeScale:FSChangeTrendMenuW]+2)*(weight-1), 0, [ShiPei CGFloatMakeScale:FSChangeTrendMenuW], [ShiPei CGFloatMakeScale:FSChangeTrendMenuH])];
    [tabItem setText:text];
    [tabItem setTextAlignment:NSTextAlignmentCenter];
    [tabItem setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:28]]];
    tabItem.userInteractionEnabled=YES;
    UITapGestureRecognizer *click =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tabItemTouchEvent:)];
    [tabItem addGestureRecognizer:click];
    tabItem.tag = weight - 1;
    [self addSubview:tabItem];
    switch (tabItem.tag) {
        case 0:
            self->TabItem1=tabItem;
            break;
        case 1:
            self->TabItem2=tabItem;
            break;
        case 2:
            self->TabItem3=tabItem;
            break;
        default:
            break;
    }
}

//设置触摸事件
- (void)tabItemTouchEvent:(id)sender{
    UITapGestureRecognizer *singleTap = (UITapGestureRecognizer *)sender;
    int tabTag=[[NSString stringWithFormat: @"%ld", (long)[singleTap view].tag] intValue];
    if ([self.menuDelegate respondsToSelector:@selector(switchChangeTrendByTabItem:originalTab:destinationTab:)]) {
        [self.menuDelegate switchChangeTrendByTabItem:self originalTab:tabTag destinationTab:tabTag];
    }
    [self changeTabItemStyle:tabTag];
}

-(void)changeTabItemStyle:(NSInteger)tag{
    switch (tag) {
        case 0:
            [self->TabItem1 setTextColor:[UIColor whiteColor]];
            [self->TabItem2 setTextColor:FUIColorFromRGB(0x444444)];
            [self->TabItem3 setTextColor:FUIColorFromRGB(0x444444)];
            [self->TabItem1 setBackgroundColor:FUIColorFromRGB(0x247bcf)];
            [self->TabItem2 setBackgroundColor:FUIColorFromRGB(0xdddddd)];
            [self->TabItem3 setBackgroundColor:FUIColorFromRGB(0xdddddd)];
            break;
        case 1:
            [self->TabItem2 setTextColor:[UIColor whiteColor]];
            [self->TabItem1 setTextColor:FUIColorFromRGB(0x444444)];
            [self->TabItem3 setTextColor:FUIColorFromRGB(0x444444)];
            [self->TabItem2 setBackgroundColor:FUIColorFromRGB(0x247bcf)];
            [self->TabItem1 setBackgroundColor:FUIColorFromRGB(0xdddddd)];
            [self->TabItem3 setBackgroundColor:FUIColorFromRGB(0xdddddd)];
            break;
        case 2:
            [self->TabItem3 setTextColor:[UIColor whiteColor]];
            [self->TabItem2 setTextColor:FUIColorFromRGB(0x444444)];
            [self->TabItem1 setTextColor:FUIColorFromRGB(0x444444)];
            [self->TabItem3 setBackgroundColor:FUIColorFromRGB(0x247bcf)];
            [self->TabItem2 setBackgroundColor:FUIColorFromRGB(0xdddddd)];
            [self->TabItem1 setBackgroundColor:FUIColorFromRGB(0xdddddd)];
            break;
        
        default:
            break;
    }
}
@end